package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"shopping/models"
	"strconv"
	"time"
)

type ShoppingController struct {
	beego.Controller
}

type Json struct {
	Code int
	Msg  string
	Data interface{}
}

func (c *ShoppingController) Add() {
	name := c.GetString("name")
	typed := c.GetString("typed")
	img := c.GetString("img")
	intro := c.GetString("intro")
	address := c.GetString("address")

	shop := models.Shopping{
		Name:    name,
		Typed:   typed,
		Img:     img,
		Intro:   intro,
		Address: address,
	}

	err := models.DB.Create(&shop).Error
	if name == "" || address == "" {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "名称,地址不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}

	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "添加成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}

func (c *ShoppingController) Upload() {
	file, header, err := c.GetFile("img")
	if err != nil {
		c.Data["json"] = Json{
			Code: 500,
			Msg:  "文件获取失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	defer file.Close()
	path := "static/img/" + header.Filename
	if header.Size > 2*1024*1024 {
		c.Data["json"] = Json{
			Code: 500,
			Msg:  "文件超过2MB",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	err = c.SaveToFile("img", path)
	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "上传失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		imgSrc := "http://127.0.0.1:8080/" + path
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "上传成功",
			Data: imgSrc,
		}
		c.ServeJSON()
	}
}

func (c *ShoppingController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 5)
	name := c.GetString("name")
	key := "page-" + strconv.Itoa(page) + "-" + name
	jsonData := make(map[string]interface{})
	val, err := models.Rdb.Get(key).Result()
	quick := models.DB.Model(&models.Shopping{})
	if name != "" {
		quick = quick.Where("name like ?", "%"+name+"%")
	}

	if err != nil {
		var count int64
		err = quick.Count(&count).Error
		var shopping []models.Shopping
		offset := (page - 1) * size
		err = quick.Limit(size).Offset(offset).Find(&shopping).Error
		if err != nil {
			c.Data["json"] = Json{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			jsonData["count"] = count
			jsonData["page"] = page
			jsonData["shopping"] = shopping

			res, _ := json.Marshal(&jsonData)
			value := string(res)
			models.Rdb.Set(key, value, time.Second*20)

			if err != nil {
				c.Data["json"] = Json{
					Code: 200,
					Msg:  "查询成功-mysql",
					Data: jsonData,
				}
				c.ServeJSON()
				return
			}
		}
	} else {
		json.Unmarshal([]byte(val), &jsonData)
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: jsonData,
		}
		c.ServeJSON()
		return
	}
}

func (c *ShoppingController) Info() {
	id, _ := c.GetInt("id")
	key := "list-" + strconv.Itoa(id)
	jsonData := make(map[string]interface{})
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var shopping models.Shopping
		err = models.DB.Where("ID =?").First(shopping).Error
		if err != nil {
			c.Data["json"] = Json{
				Code: 0,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {

			res, _ := json.Marshal(&jsonData)
			value := string(res)
			models.Rdb.Set(key, value, time.Second*20)

			if err != nil {
				c.Data["json"] = Json{
					Code: 200,
					Msg:  "查询成功-mysql",
					Data: jsonData,
				}
				c.ServeJSON()
				return
			}
		}
	} else {
		json.Unmarshal([]byte(val), &jsonData)
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: jsonData,
		}
		c.ServeJSON()
		return
	}
}

func (c *ShoppingController) Del() {
	id, _ := c.GetInt("id")
	err := models.DB.Where("ID = ?", id).Error
	if err != nil {
		c.Data["json"] = Json{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = Json{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.ServeJSON()
	}
}
